$ ->
    $view = $('.view-handle-topic')
    $submit = $('.submit', $view)

    $submit.on 'click', (e)->
        _id = $view.find('[name=_id]').val()
        title = _.trim $view.find('[name=title]').val()
        content = _.trim $view.find('[name=content]').val()

        if _id
            q = W.api.put "topic/#{_id}/edit", {title, content}
            q.then ->
                swal({title: "修改成功!", type: "success"}, ()-> location.href="/topic/#{_id}")
        else
            q = W.api.post "topic/new", {title, content}
            q.then (r)->
                console.log r
                swal({title: "添加成功!", type: "success"}, ()-> location.href="/")

        q.catch W.alertAjaxError